package net.lym.es.doc;

import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;

/**
 * ES测试 : 文档数据批量删除
 */
public class ESTest_Doc_Delete_Batch {
    public static void main(String[] args) throws Exception {
        // 创建 ES 客户端
        RestHighLevelClient esClient = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost", 9200, "http"))
        );

        // 创建批量删除请求对象
        BulkRequest request = new BulkRequest();
        // 构造批量删除数据
        request.add(new DeleteRequest().index("user").id("1001"));
        request.add(new DeleteRequest().index("user").id("1002"));
        request.add(new DeleteRequest().index("user").id("1003"));
        // 客户端发送请求，获取响应对象
        BulkResponse response = esClient.bulk(request, RequestOptions.DEFAULT);
        // 打印结果信息
        System.out.println("took:" + response.getTook());
        System.out.println("items:" + response.getItems());

        // 关闭 ES 客户端
        esClient.close();
    }
}
